package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.TenderInStorageItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageTenderOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageTenderOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;


//招采入库概览
public class InStorageTenderOverviewActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView tenderInStorageItemList;

    private List<InStorageTenderOverviewBean.DataBean.FreeInStorageListBean> allData;

    private TenderInStorageItemListAdapter adapter;

    private String mPurchase, mMaterielCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_tender_overview);
        initView(InStorageTenderOverviewActivity.this);
        SPUtils.removeTenderInStorageInformation(InStorageTenderOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        mMaterielCode = "";
        initData(InStorageTenderOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        tenderInStorageItemList = (PullableListView) activity.findViewById(R.id.tenderInStorageItemList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.tenderInStorageItemListRefresh);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText("招采入库概览");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_in_storage_receive_list_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        tenderInStorageItemList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    Intent intent = new Intent(activity, InStorageTenderAddOperatorActivity.class);
                    startActivity(intent);
                }
            }
        });
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private boolean check(Context context) {
//        String reasonValue = InStorageUtils.checkFreeReason(context);
//        if (TextUtils.isEmpty(reasonValue)) {
//            Utils.middleToast(context, "请选择免检原因");
//            return false;
//        }

        int confirmNumber = InStorageUtils.getTenderInStorageConfirmNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, "请至少完善一个行项目");
            return false;
        }
        return true;
    }

    private void getExtra(Activity activity) {
        mPurchase = activity.getIntent().getStringExtra(InStorageExtra.PURCHASE_ORDER);
    }

    private void addInStorageTenderItemListData(Context context, InStorageTenderOverviewBean.DataBean bean, List<InStorageTenderOverviewBean.DataBean.FreeInStorageListBean> tenderInStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new TenderInStorageItemListAdapter(context, bean, tenderInStorageItemList, mMaterielCode, (String materielCode) -> {
                mMaterielCode = materielCode;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context) {
        String tenderInStorageInformation = SPUtils.getTenderInStorageInformation(context);
        if (TextUtils.isEmpty(tenderInStorageInformation)) {
            InStorageTenderOverviewRequest.request(context, mPurchase, new GsonBeanListener<InStorageTenderOverviewBean>() {
                @Override
                public void onSuccess(InStorageTenderOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, InStorageTenderOverviewBean.class);
                    SPUtils.saveTenderInStorageInformation(context, json);
                    InStorageTenderOverviewBean.DataBean data = bean.getData();
                    allData = bean.getData().getFreeInStorageList();
                    addInStorageTenderItemListData(context, data, ActivityUtils.getShowData(allData, 0), tenderInStorageItemList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            InStorageTenderOverviewBean bean = gson.fromJson(tenderInStorageInformation, InStorageTenderOverviewBean.class);
            if (TextUtils.isEmpty(mMaterielCode)) {
                allData = bean.getData().getFreeInStorageList();
            } else {
                allData = InStorageUtils.getTenderInStorageList(context, mMaterielCode);
            }
            addInStorageTenderItemListData(context, bean.getData(), ActivityUtils.getShowData(allData, 0), tenderInStorageItemList, true);
        }
    }

    public interface InStorageTenderOverviewListener {
        void onNoDoubleClickListener(String materielCode);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(InStorageTenderOverviewActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                InStorageTenderOverviewActivity.super.onBackPressed();
            }
        }).show();
    }
}
